package com.vip.sys.modular.login.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vip.sys.modular.login.entity.LoginMonitor;
import com.vip.sys.modular.login.service.LoginMonitorService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author ASUS
 * @Date 2025/4/29 13:44
 * @Descripition TODO 定期清理登录日志
 */
@Component
@Slf4j
public class ClearLoginLogJobHandler {
    @Resource
    private LoginMonitorService loginMonitorService;

    @Scheduled(cron = "0 30 9 1 * ?") // 每月1号9:30执行
//    @Scheduled(cron = "0 * * * * ?")
    public void clearLoginLog() {
        log.info("开始清理上个月登录日志");
        LambdaQueryWrapper<LoginMonitor> queryWrapper = new LambdaQueryWrapper<>();
        // 30天前
        queryWrapper.lt(LoginMonitor::getLoginTime, System.currentTimeMillis() - 30L * 24 * 60 * 60 * 1000);
        // 删除
        loginMonitorService.remove(queryWrapper);
        log.info("清理登录日志完成");
    }

}
